package mcg.android.superlista.produto;

import mcg.android.superlista.R;
import mcg.android.superlista.db.DBAdapter;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CatalogOverview extends ListActivity {
	private DBAdapter dbAdapter;
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
	private static final int DELETE_ID = Menu.FIRST + 1;
	private Cursor cursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.catalog_layout);
		this.getListView().setDividerHeight(2);
		dbAdapter = new DBAdapter(this);
		dbAdapter.open();
		fillData();
		registerForContextMenu(getListView());
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		dbAdapter.close();
	}
	
	
	// Create the menu based on the XML defintion
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.catalog_menu, menu);
		return true;
	}

	// Reaction to the menu selection
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_prod:
			createProduct();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.new_prod:
			createProduct();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			dbAdapter.deleteProduct(info.id);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	private void createProduct() {
		Intent i = new Intent(this, ProductDetails.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	// ListView and view (row) on which was clicked, position and
	@Override
	protected void onListItemClick(ListView l, View v, int position, final long id) {
		super.onListItemClick(l, v, position, id);
		
		Cursor product = dbAdapter.fetchProduct(id);
		startManagingCursor(product);
		
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		
//		LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
//		View viewProdutoEdit = inflater.inflate(R.layout.product_edit, (ViewGroup) findViewById(R.id.layout_detail_produto));
//
//		final EditText editProduto = (EditText) viewProdutoEdit.findViewById(R.id.product_edit_name);
		final EditText editProduto = new EditText(this);

		editProduto.setText(product.getString(product
				.getColumnIndexOrThrow(DBAdapter.KEY_NAME)));
		
		alert.setPositiveButton(R.string.product_edit_confirm, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dbAdapter.updateProduct(id, editProduto.getText().toString());
				fillData();
				Toast.makeText(getApplicationContext(), "Produto salvo.", Toast.LENGTH_SHORT).show();
			}
		});

		alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
			
		
		alert.setView(editProduto);
		alert.setTitle(R.string.product_name);
		alert.show();
		
	}

	// Called with the result of the other activity
	// requestCode was the origin request code send to the activity
	// resultCode is the return code, 0 is everything is ok
	// intend can be use to get some data from the caller
	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		fillData();

	}

	private void fillData() {
		cursor = dbAdapter.fetchAllProducts();
		startManagingCursor(cursor);

		String[] from = new String[] { DBAdapter.KEY_NAME };
		int[] to = new int[] { R.id.label };

		// Now create an array adapter and set it to display using our row
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.product_row_layout, cursor, from, to);
		setListAdapter(notes);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}
}
